package com.shbaiche.bifenba.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.ui.base.XStateFragmentAdapter;
import com.shbaiche.bifenba.ui.data.DataFragment;
import com.shbaiche.bifenba.ui.home.*;
import com.shbaiche.bifenba.ui.match.MatchFragment;
import com.shbaiche.bifenba.ui.my.MyFragment;
import com.shbaiche.bifenba.ui.video.VideoFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.XActivity;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import devlight.io.library.ntb.NavigationTabBar;

public class MainTabActivity extends XActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @BindView(R.id.ntb_horizontal)
    NavigationTabBar navigationTabBar;

    List<Fragment> fragmentList = new ArrayList<>();
    XFragmentAdapter adapter;

    @Override
    public void initData(Bundle savedInstanceState) {


        initUI();
    }

    private void initUI() {
        fragmentList.clear();
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null && fragments.size() > 0) {
            fragmentList.clear();
            for (Fragment fragment : fragments) {
                if (fragment instanceof Fragment) {
                    fragmentList.add(fragment);
                }
            }
        }

        if(fragmentList.size() == 0){
            fragmentList.add(HomeActivity.newInstance());
            fragmentList.add(VideoFragment.newInstance());
            fragmentList.add(MatchFragment.newInstance());
            fragmentList.add(DataFragment.newInstance());
            fragmentList.add(MyFragment.newInstance());
        }



        if (adapter == null) {
            adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, null);
        }
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(5);


        final String[] colors = getResources().getStringArray(R.array.preview_colors);

//        navigationTabBar.setTitleMode(NavigationTabBar.TitleMode.ACTIVE);
//        navigationTabBar.setBadgeGravity(NavigationTabBar.BadgeGravity.BOTTOM);
//        navigationTabBar.setBadgePosition(NavigationTabBar.BadgePosition.CENTER);
//        //navigationTabBar.setTypeface("fonts/custom_font.ttf");
//        navigationTabBar.setIsBadged(true);
//        navigationTabBar.setIsTitled(true);
//        navigationTabBar.setIsTinted(true);
//        navigationTabBar.setIsBadgeUseTypeface(true);
        navigationTabBar.setBadgeBgColor(Color.RED);
        navigationTabBar.setBadgeTitleColor(Color.WHITE);
//        navigationTabBar.setIsSwiped(true);
        navigationTabBar.setBgColor(Color.WHITE);
        //navigationTabBar.setBadgeSize(10);
        //navigationTabBar.setTitleSize(10);
        navigationTabBar.setIconSizeFraction(0.45f);

        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_home),
                        Color.parseColor(colors[0]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_home_pass))
                        .title("首页")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_video),
                        Color.parseColor(colors[1]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_video_pass))
                        .title("视频")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_match),
                        Color.parseColor(colors[2]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_match_pass))
                        .title("比分")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_data),
                        Color.parseColor(colors[3]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_data_pass))
                        .title("数据")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_tab_my),
                        Color.parseColor(colors[4]))
                        .selectedIcon(getResources().getDrawable(R.drawable.ic_tab_my_pass))
                        .title("我的")
                        .build()
        );

        navigationTabBar.setModels(models);
        navigationTabBar.setViewPager(viewPager, 0);
        navigationTabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                navigationTabBar.getModels().get(position).hideBadge();
            }

            @Override
            public void onPageScrollStateChanged(final int state) {

            }
        });

//        navigationTabBar.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
//                    final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
//                    navigationTabBar.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            model.showBadge();
//                        }
//                    }, i * 100);
//                }
//            }
//        }, 500);


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_tab;
    }
}
